package cn.initcap.concurrency.concurrency.atomic;

import cn.initcap.concurrency.annoations.ThreadSafe;
import cn.initcap.concurrency.concurrency.ThreadPoolCreatHelper;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.extern.slf4j.Slf4j;

/**
 * 这个适合方法无论如何只执行一次。
 *
 * @author initcap
 * @date Created in 2018/6/13 PM9:34.
 */
@Slf4j
@ThreadSafe
public class AtomicBooleanExample {

    /**
     * 总请求数
     */
    private static final int CLIENT_TOTAL = 5000;
    /**
     * 同时并发执行的线程数
     */
    private static final int THREAD_TOTAL = 200;
    private static AtomicBoolean isHappened = new AtomicBoolean(false);

    public static void main(String[] args) throws Exception {

        ThreadPoolCreatHelper.threadPoolRun(CLIENT_TOTAL, THREAD_TOTAL,
                AtomicBooleanExample.class,
                "test", "AtomicBooleanExample");

        log.info("isHappened:{}", isHappened.get());
    }

    public static void test() {
        if (isHappened.compareAndSet(false, true)) {
            log.info("execute");
        }
    }
}
